var FileCheckFunction = null;
var prgm = null;
var timeout = null;

function NanoCompile()
{
	var result = document.getElementById('mc_nano_out');
	var rescode = document.getElementById('mc_nano_code');
	result.value = '';
	rescode.value = '';
	setTimeout(function() {
	var code = document.getElementById('mc_nano_editor').value;
	GetPageContent('POST', 'nano/nanocompiler.php', 'code=' + encodeURIComponent(code), NanoCheckCompile, null, true); }, 100);
}

function NanoCheckCompile(success, text, param)
{
	var result = document.getElementById('mc_nano_out');
	var code = document.getElementById('mc_nano_code');
	if(success == false)
	{
		result.value = 'Erreur : impossible de se connecter au serveur';
		code.value = '';
		return;
	}
	var data = text.split(':::');
	result.value = data[0];
	if(data[1]) code.value = data[1];
}

function NanoNewPrgm()
{
	document.getElementById('mc_nano_editor').value = '';
}

function NanoSavePrgm()
{
	NanoHideNotif();
	NanoOpenDlg();
	var prgm = document.getElementById('mc_nano_editor').value;
	if(prgm.length >= 16384) { alert('16 ko maximum'); NanoCloseDlg(); return false; }
	if(prgm.length == 0) { alert('Le programme est vide'); NanoCloseDlg(); return false; }
	GetPageContent('GET', 'nano/nanofile.php?do=list', '', NanoCheckSave, prgm, false);	
}

function NanoCheckSave(success, text, param)
{
	if(success == false)
	{
		NanoSetNotif('Erreur : impossible de se connecter au serveur');
		return;
	}
	if(param == null)
	{
		NanoSetNotif(text);
		NanoCloseDlg();
	}
	else
	{
		prgm = param;
		var e = document.getElementById('nc_prgm_list');
		e.style.width = 400 + "px";
		e.style.height = "auto";
		e.style.top = (screen.availHeight/2 - 150) + "px";
		e.style.left = (screen.availWidth/2 - 200) + "px";
		e.getElementsByTagName('h3')[0].innerHTML = 'Enregistrer';
		var c = document.getElementById('nc_prgm_list_content');
		c.innerHTML = text;
		e.style.display = 'block';
		var n = document.getElementById('nc_save_new');
		n.style.display = 'block';
		FileCheckFunction = NanoSaveFile;
	}
}

function NanoSaveFile(e)
{
	var name = null;
	if(e == null)
	{
		name = document.getElementById('nc_new_name').value;
		if(name == '' || name == null) { alert('Veuillez remplir le nom du programme.'); return false; }
	}
	else
	{
		name = e.getElementsByTagName('span')[0].innerHTML;
	}
	GetPageContent('POST', 'nano/nanofile.php?do=save', 'name=' + encodeURIComponent(name) + '&prgm=' + encodeURIComponent(prgm), NanoCheckSave, null, true);
}

function NanoOpenPrgm()
{
	NanoOpenDlg();
	GetPageContent('GET', 'nano/nanofile.php?do=list', '', NanoCheckOpen, null, true);
}

function NanoCheckOpen(success, text, param)
{
	if(success == false)
	{
		NanoSetNotif('Erreur : impossible de se connecter au serveur');
		return;
	}
	var e = document.getElementById('nc_prgm_list');
	e.style.width = 400 + "px";
	e.style.height = "auto";
	e.style.top = (screen.availHeight/2 - 150) + "px";
	e.style.left = (screen.availWidth/2 - 200) + "px";
	e.getElementsByTagName('h3')[0].innerHTML = 'Ouvrir';
	var n = document.getElementById('nc_save_new');
	n.style.display = 'none';
	var c = document.getElementById('nc_prgm_list_content');
	c.innerHTML = text;
	e.style.display = 'block';
	FileCheckFunction = NanoOpenFile;
}

function NanoCheckFile(e)
{
	if(FileCheckFunction !== null)
		FileCheckFunction(e);
}

function NanoOpenFile(e)
{
	var code = e.getElementsByTagName('div')[0];
	var editor = document.getElementById('mc_nano_editor');
	editor.value = code.innerHTML;
	NanoSetNotif('Programme ouvert avec succès.');
	NanoCloseDlg();
}

function NanoOpenLastPrgm()
{
	GetPageContent('GET', 'nano/nanocompiler.php?last=1', '', NanoCheckOpenLastPrgm, null, true);
}

function NanoCheckOpenLastPrgm(success, text, param)
{
	if(success == false)
	{
		NanoSetNotif('Erreur : impossible de se connecter au serveur');
		return;
	}
	if(text == 0) NanoSetNotif('Aucun programme compilé');
	else
	{
		var editor = document.getElementById('mc_nano_editor');
		editor.value = text;
		NanoSetNotif('Programme ouvert avec succès.');
	}
}

function NanoDelPrgm()
{
	NanoOpenDlg();
	GetPageContent('GET', 'nano/nanofile.php?do=list', '', NanoCheckDel, null, true);
}

function NanoCheckDel(success, text, param)
{
	if(success == false)
	{
		NanoSetNotif('Erreur : impossible de se connecter au serveur');
		return;
	}
	var e = document.getElementById('nc_prgm_list');
	e.style.width = 400 + "px";
	e.style.height = "auto";
	e.style.top = (screen.availHeight/2 - 150) + "px";
	e.style.left = (screen.availWidth/2 - 200) + "px";
	e.getElementsByTagName('h3')[0].innerHTML = 'Supprimer';
	var n = document.getElementById('nc_save_new');
	n.style.display = 'none';
	var c = document.getElementById('nc_prgm_list_content');
	c.innerHTML = text;
	e.style.display = 'block';
	FileCheckFunction = NanoDelFile;
}

function NanoDelFile(e)
{
	var name = null;
	name = e.getElementsByTagName('span')[0].innerHTML;
	GetPageContent('POST', 'nano/nanofile.php?do=del', 'name=' + encodeURIComponent(name), NanoCheckDelFile, null, true);
}

function NanoCheckDelFile(success, text, param)
{
	if(success == false)
	{
		NanoSetNotif('Erreur : impossible de se connecter au serveur');
		return;
	}
	NanoCloseDlg();
	NanoSetNotif(text);
}

function NanoSetNotif(text)
{
	var result = document.getElementById('nc_notif');
	result.innerHTML = text;
	result.style.visibility = '';
	if(timeout != null) clearTimeout(timeout);
	timeout = setTimeout('NanoHideNotif();', 5000);
}

function NanoHideNotif()
{
	var result = document.getElementById('nc_notif');
	result.style.visibility = 'hidden';
}

function NanoOpenDlg()
{
	var e = document.getElementById('mc_dlg');
	e.style.width = screen.availWidth + "px";
	e.style.height = screen.availHeight + "px";
	e.style.display = 'block';
}

function NanoCloseDlg()
{
	var e = document.getElementById('mc_dlg');
	e.style.display = 'none';
	ToggleDisplay('nc_prgm_list');
}